﻿#region

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Radovici.Ctrls.ContentControls;
using YouTubePlaylist.MusicVideoService;
using YouTubePlaylist.ViewModels;

#endregion

namespace YouTubePlaylist.Views
{
    public partial class PlaylistToolbarCtrl
    {
        public PlaylistToolbarCtrl()
        {
            InitializeComponent();
        }

        public PlaylistViewModel ViewModel
        {
            get { return DataContext as PlaylistViewModel; }
        }

        //private void OnPlaylistToolbarCtrlLoaded(object sender, RoutedEventArgs e)
        //{
        //    var propertyChanged = DataContext as INotifyPropertyChanged;
        //    if (propertyChanged != null)
        //    {
        //        propertyChanged.PropertyChanged += OnPropertyChanged;
        //    }
        //}

        //private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        //{
        //    switch (e.PropertyName)
        //    {
        //        case "Artists":
        //            Dispatcher.BeginInvoke(delegate { ArtistCtrl.PopulateComplete(); });
        //            break;
        //        case "ArtistTracks":
        //            Dispatcher.BeginInvoke(delegate { TrackCtrl.PopulateComplete(); });
        //            break;
        //    }
        //}

        //private void OnQuickTrackKeyUp(object sender, KeyEventArgs e)
        //{
        //    var txt = sender as AutoCompleteBox;
        //    if (txt != null)
        //    {
        //        switch (e.Key)
        //        {
        //            case Key.Enter:
        //                AddTrack();
        //                break;
        //            default:
        //                if (e.Key >= Key.A && e.Key <= Key.Z)
        //                {
        //                    ViewModel.AutoComplete.GetTracks(txt.Text);
        //                }
        //                break;
        //        }
        //    }
        //}

        private void OnAddTrackAdvanced(object sender, RoutedEventArgs e)
        {
            var widget = new Widget
                             {
                                 Content = new TrackInputViewModel(new Track()).LoadView(), //new TrackInputCtrl(new VideoTrack()),
                                 Title = "New Track",
                                 OkEnabled = true
                             };
            widget.Show(OnTrackAdded, sender as UIElement);
        }

        private void OnTrackAdded(object ctrl, EventArgs e)
        {
            var inputCtrl = ctrl as TrackInputCtrl;
            if (inputCtrl != null)
            {
                var vm = inputCtrl.DataContext as TrackInputViewModel;
                if (vm != null)
                {
                    ViewModel.PlaylistModel.Add(vm.Track);
                }
            }
        }

        private void OnAddTrack(object sender, RoutedEventArgs e)
        {
            AddTrack();
        }

        private void OnAddTrackInline(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                var track = btn.Tag as string;
                if (track != null)
                {
                    AddTrack(track);
                }
            }
        }

        private void AddTrack()
        {
            AddTrack(TrackInput.Text);
        }

        private void AddTrack(string input)
        {
            if (string.IsNullOrEmpty(input)) return;
            int byIndex, byNextIndex = -1;
            do
            {
                byIndex = byNextIndex;
                byNextIndex = input.IndexOf(" by ", byIndex + 1);
            } while (byNextIndex != -1);
            if (byIndex > 0)
            {
                string title = input.Substring(0, byIndex);
                string artist = input.Substring(byIndex + " by ".Length);
                ViewModel.PlaylistModel.SaveTrack(new VideoTrack {Artist = artist, Name = title});
            }
        }

        private void OnDropDownOpening(object sender, RoutedPropertyChangingEventArgs<bool> e)
        {
            var txt = sender as AutoCompleteBox;
            if (txt != null)
            {
            }
        }
    }
}